<?php


namespace app\admin\model;


use app\sys\service\AliOss;
use think\Db;
use think\Exception;
use think\facade\Env;
use think\Model;

class RobotSet extends Model
{

//    public function getStartTimeAttr($value)
//    {
//        if (empty($value)) {
//            return "";
//        }
//        return date("H:i", $value);
//    }
//
//    public function getEndTimeAttr($value)
//    {
//        if (empty($value)) {
//            return "";
//        }
//        return date("H:i", $value);
//    }

    public function getIconAttr($value)
    {
        if (empty($value)) {
            return "";
        }
        $url = Env::get("ALIOSS.OSS_URL");
        return $url . $value . "?time=" . time();
    }

    public static function saveOne($name, $icon, $sex, $time)
    {
        $div = Env::get("ALIOSS.OSS_DIV");
        $path = $div . "/robot.jpg";

        if ($icon) {
            $im = $icon->getInfo();
            $a = new AliOss();
            $a->oss_delete_file($path);
            $a->oss_upload_content($path, $im["tmp_name"]);
        }

        try {
            Db::startTrans();
            $ids = RobotSet::column("id");
            RobotSet::where("id", "in", $ids)->delete();

            $jTime = json_encode($time);
            RobotSet::create(["name" => $name, "icon" => $path, "sex" => $sex, "time" => $jTime]);

            $s = Staff::field("id")->where("staff_no", "PXS000000")->find();
            if (!$s) {
                staff::create(["staff_no" => "PXS000000", "staff_name" => $name, "icon" => $path]);
            } else {
                staff::where("id", $s["id"])->update(["staff_no" => "PXS000000", "staff_name" => $name, "icon" => $path]);
            }
            Db::commit();
        } catch (Exception $exception) {
            Db::rollback();
            return $exception->getMessage();
        }
        return "";
    }

    public static function timeOut(): bool
    {
        $set = RobotSet::field("time")->find();
        $t = date("Hi", time());
        if ($set) {
            if (!empty($set["time"])) {
                $timeArr = json_decode($set["time"]);
                foreach ($timeArr as $k => $v) {
                    $s = date("Hi", $v[0]);
                    $e = date("Hi", $v[1]);

                    if ($s <= $t && $e > $t) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    // 打开聊天窗口
    public static function openWind($userNo, $staffNo)
    {
        // 行为反馈
        $title = "打开聊天窗口";
        $status = RobotAction::where("title", $title)->value("status");
        // 机器人时间
        if (self::timeOut() && $status) {
            // 虚拟
            RobotAction::actionToUserOne($userNo, $staffNo, $title);
        } else {
            // 系统(自动回复)
            Reply::touchOffAutoReply($userNo, $staffNo);
        }
    }

    // 匹配不到回复
    public static function matchRe($userNo, $staffNo, $data)
    {
        // 虚拟
        $title = "匹配不到回复";
        $status = RobotAction::where("title", $title)->value("status");
        if (self::timeOut() && $status && $data) {
            $res = Qu::ifQu($data);
            if ($res) {
                // 有对应数据回复
                RobotAction::actionToUserOne($userNo, $staffNo, "", $res);
            } else {
                RobotAction::actionToUserOne($userNo, $staffNo, $title);


            }
        }
        // 没有正常执行
    }

    // 店铺匹配数量0
    public static function matchStore($userNo, $staffNo, $data)
    {
        $title = "店铺匹配数量=0";
        $status = RobotAction::where("title", $title)->value("status");
        if (self::timeOut() && $status) {
            RobotAction::actionToUserOne($userNo, $staffNo, $title);
        }

    }

    // 店铺匹配数量>0
    public static function matchStoreMore($userNo, $staffNo, $data)
    {
        $title = "店铺匹配数量>0";
        $status = RobotAction::where("title", $title)->value("status");
        if (self::timeOut() && $status) {
            RobotAction::actionToUserOne($userNo, $staffNo, $title);
        }

    }

    public static function GetSet()
    {
        $data = RobotSet::field("*")->find();
        $data["time"] = json_decode($data["time"]);
        return $data;
    }

}